﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using BrainTechLLC;
using BrainTechLLC.WPF;
using System.Windows.Data;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows;
using System.Collections.Specialized;
using DirectoryViewModels;
using System.Threading;
using System.ComponentModel;

namespace BrainTechLLC.FolderExplorer
{
    public class AltGrid : Grid
    {
        public static readonly DependencyProperty FolderProperty =
            DependencyProperty.Register("Folder", typeof(FolderViewModel), typeof(AltGrid), new PropertyMetadata(new FolderViewModel(null, null) { FullPath = @"C:\" }, OnFolderPropertyChanged));

        public FolderViewModel Folder
        {
            get { return GetValue(FolderProperty) as FolderViewModel; }
            set { SetValue(FolderProperty, value); }
        }

        public static readonly DependencyProperty IsFolderSelectedProperty =
            DependencyProperty.Register("IsFolderSelected", typeof(bool), typeof(AltGrid), new PropertyMetadata(false, OnIsFolderSelectedPropertyChanged));

        public bool IsFolderSelected
        {
            get { return (bool)GetValue(IsFolderSelectedProperty); }
            set { SetValue(IsFolderSelectedProperty, value); }
        }

        private static void OnIsFolderSelectedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AltGrid source = d as AltGrid;
            source.UpdateIsFolderSelected((bool)e.NewValue, (bool)e.OldValue);
        }

        public void UpdateIsFolderSelected(bool newIsFolderSelected, bool oldIsFolderSelected)
        {
            IsFolderSelected = newIsFolderSelected;
            //OnPropertyChanged(new DependencyPropertyChangedEventArgs(IsFolderSelectedProperty, oldIsFolderSelected, newIsFolderSelected));
        }

        public static readonly DependencyProperty DepthProperty =
            DependencyProperty.Register("Depth", typeof(int), typeof(AltGrid), new PropertyMetadata(0, OnDepthPropertyChanged));

        public int Depth
        {
            get { return (int)GetValue(DepthProperty); }
            set { SetValue(DepthProperty, value); }
        }

        private static void OnDepthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AltGrid source = d as AltGrid;
            source.UpdateDepth((int)e.NewValue, (int)e.OldValue);
        }

        public void UpdateDepth(int newDepth, int oldDepth)
        {
            Depth = newDepth;
            //OnPropertyChanged(new DependencyPropertyChangedEventArgs(DepthProperty, oldDepth, newDepth));
        }

        private static void OnFolderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AltGrid source = d as AltGrid;
            source.UpdateFolder(e.NewValue as FolderViewModel, e.OldValue as FolderViewModel);
        }

        public void UpdateFolder(FolderViewModel newFolder, FolderViewModel oldFolder)
        {
            if (oldFolder != null)
            {
                oldFolder.PropertyChanged -= newFolder_PropertyChanged;
            }

            if (newFolder != null)
            {
                newFolder.PropertyChanged += new PropertyChangedEventHandler(newFolder_PropertyChanged);
            }
            Folder = newFolder;
            if (newFolder != null)
            {
                Depth = newFolder.Depth;
            }
            OnPropertyChanged(new DependencyPropertyChangedEventArgs(FolderProperty, oldFolder, newFolder));
        }

        void newFolder_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsSelected")
            {
                FolderViewModel f = sender as FolderViewModel;
                IsFolderSelected = f.IsSelected;
            }
            else if (e.PropertyName == "Depth")
            {
                FolderViewModel f = sender as FolderViewModel;
                Depth = f.Depth;
            }
        }
    }
}
